package com.ks;

import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
import com.alicp.jetcache.anno.config.EnableMethodCache;
import com.ks.model.User;
import com.ks.service.UserService;
import com.ks.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/")
@SpringBootApplication
@EnableMethodCache(basePackages = "com.ks")
@EnableCreateCacheAnnotation
public class MySpringBootApp {
    public static void main(String[] args) {
        ApplicationContext applicationContext =SpringApplication.run(MySpringBootApp.class);
        UserService userService = applicationContext.getBean("userService", UserService.class);
        System.out.println("----------启动--ok");
    }

    @Autowired
    public UserService userService ;

    @RequestMapping("/get")
    public String get(@RequestParam Long id){
        User user = userService.getUser(id);
        return "hello cache=="+ StringUtils.toStr( user);
    }
    @RequestMapping("/get/{id}")
    public String getById(@PathVariable("id") Long id){
        User user = userService.getUser(id);
        return "hello cache=="+ StringUtils.toStr( user);
    }
    @RequestMapping("/delete")
    public String delete(@RequestParam Long id){
        boolean b = userService.deleteCache(id);
        return "hello cache==，b======="+b;
    }
    @RequestMapping("/delete/{id}")
    public String deleteById(@PathVariable("id") Long id){
        boolean b = userService.deleteCache(id);
        return "hello cache==，b======="+b;
    }
    @RequestMapping("/list")
    public String list(){
        List<User> list = userService.list();
        return "hello cache==，list======="+list.toString();
    }

    //http://localhost:8080/get/1   基于成员变量缓存
    //http://localhost:8080/delete/1  手动清理成员变量缓存
    //http://localhost:8080/list  基于方法缓存
}